home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / comm / ums / developer / interfaces / c / examples / sumnew.c < prev   
Encoding:
C/C++ Source or Header  |  1993-08-01  |  6.8 KB  |  260 lines

  1. /************************************************************************/
  2. /*         SumNew:  Print a summary of new messages to stdout.          */
  3. /************************************************************************/
  4.  
  5. /*
  6.  * Demo sent by: SteveX@omx.OCUnix.On.Ca (Steve Tibbett)
  7.  *
  8.  * Adapted to DICE by stefanb
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <exec/types.h>
  16. #include <clib/ums_protos.h>
  17. #include <clib/exec_protos.h>
  18. #include <pragmas/ums_pragmas.h>
  19. #include <pragmas/exec_pragmas.h>
  20. #include <dos/dos.h>
  21.  
  22. UMSUserAccount Handle;
  23. struct Library *UMSBase;
  24. extern struct Library *SysBase;
  25.  
  26. struct Subject
  27.         {
  28.         struct Subject *Next;
  29.         long Count;
  30.         char *Text;
  31.         } *Subjects;
  32.  
  33. /************************************************************************/
  34. /*           This gets called just before the program exits.            */
  35. /************************************************************************/
  36. void
  37. cleanup(void)
  38. {
  39. if (Handle) UMSLogout(Handle);
  40. if (UMSBase) CloseLibrary(UMSBase);
  41. }
  42.  
  43. /************************************************************************/
  44. /*             This gets jumped to whenever Ctrl-C is hit.              */
  45. /************************************************************************/
  46. void
  47. breaker(void)
  48. {
  49. printf("^C\n");
  50. fflush(stdout);
  51. }
  52.  
  53. /************************************************************************/
  54. /*               If there is an error, print it and exit.               */
  55. /************************************************************************/
  56. void
  57. CheckErr(void)
  58. {
  59. UMSError err;
  60.  
  61. err=UMSErrNum(Handle);
  62.  
  63. if (err != UMSERR_OK)
  64.         {
  65.         fprintf(stderr,"UMS-Error: %3d, '%s'\n",err,UMSErrTxt(Handle));
  66.         exit(20);
  67.         }
  68. }
  69.  
  70. /************************************************************************/
  71. /*                Add this subject to the subject list.                 */
  72. /************************************************************************/
  73. void
  74. AddSubjectToList(char *SubText)
  75. {
  76. struct Subject *Sub;
  77. struct Subject *NewSub;
  78.  
  79. Sub=Subjects;
  80. while (Sub)
  81.         {
  82.         if (stricmp(SubText, Sub->Text)==0)
  83.                 {
  84.                 Sub->Count++;
  85.                 return;
  86.                 };
  87.  
  88.         Sub=Sub->Next;
  89.         };
  90.  
  91. NewSub=malloc(strlen(SubText)+1+sizeof(struct Subject));
  92. if (NewSub==0) return;
  93.  
  94. NewSub->Text=(char *)(NewSub+1);
  95. strcpy(NewSub->Text, SubText);
  96. NewSub->Count=1;
  97.  
  98. if (Subjects==0)
  99.         {
  100.         Subjects=NewSub;
  101.         NewSub->Next=0;
  102.         } else
  103.         {
  104.         if (stricmp(NewSub->Text, Subjects->Text)<0)
  105.                 {
  106.                 NewSub->Next=Subjects;
  107.                 Subjects=NewSub;
  108.                 } else
  109.                 {
  110.                 Sub=Subjects;
  111.                 while (Sub->Next)
  112.                         {
  113.                         if (stricmp(NewSub->Text, Sub->Next->Text)<0)
  114.                                 {
  115.                                 NewSub->Next=Sub->Next;
  116.                                 Sub->Next=NewSub;
  117.                                 break;
  118.                                 };
  119.  
  120.                         Sub=Sub->Next;
  121.                         };
  122.  
  123.                 if (Sub->Next==0)
  124.                         {
  125.                         Sub->Next=NewSub;
  126.                         NewSub->Next=0;
  127.                         };
  128.                 };
  129.         };
  130. }
  131.  
  132. /************************************************************************/
  133. /*                      Main entry/exit and loop                        */
  134. /************************************************************************/
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. char *login,*passwd;
  139. UMSMsgNum result;
  140. char SubjLine[256];
  141.  
  142. if (argc!=3)
  143.         {
  144.         printf("Usage: %s \"User Name\" \"Password\"\n", argv[0]);
  145.         exit(0);
  146.         };
  147.  
  148. onbreak(breaker);
  149.  
  150. UMSBase=OpenLibrary("ums.library", 9);
  151. if (UMSBase==0)
  152.         {
  153.         fprintf(stderr, "Error: Can't open ums.library.\n");
  154.         exit(0);
  155.         };
  156.  
  157. login=argv[1];
  158. passwd=argv[2];
  159.  
  160. if ((Handle=UMSLogin(login,passwd))==NULL)
  161.         {
  162.         fprintf(stderr,"UMS login failed.\n");
  163.         exit(20);
  164.         }
  165.  
  166. /****************************************************************************
  167.  
  168.    1. Flag all new messages.
  169.  
  170.       If: (GlobalFlags & (ViewAccess|PostPoned|Old)) == ViewAccess
  171.  
  172.       Set the local flag bit 0.
  173.  
  174.       These are all NEW messages, which can be seen by the user.
  175.  
  176. *****************************************************************************/
  177.  
  178. printf("Searching...");
  179. fflush(stdout);
  180.  
  181. result=UMSSelectTags(Handle,
  182.         UMSTAG_SelMask,         UMSUSTATF_ViewAccess|UMSUSTATF_PostPoned|UMSUSTATF_Old,
  183.         UMSTAG_SelMatch,        UMSUSTATF_ViewAccess,
  184.         UMSTAG_SelWriteLocal,   TRUE,
  185.         UMSTAG_SelSet,          (1L<<0),
  186.         UMSTAG_SelUnset,        0,
  187.         TAG_DONE);
  188.  
  189. CheckErr();
  190. if (result==0)
  191.         printf("No new messages.");
  192.         else printf("%d new messages found.  Summarizing...\n", result);
  193.  
  194.  
  195. /****************************************************************************
  196.  
  197.    2. Read in all the subjects.
  198.  
  199. *****************************************************************************/
  200.  
  201. if (result)
  202.         {
  203.         long Succ;
  204.         long HdrLength;
  205.         long TextLength;
  206.         char *Subject;
  207.         char *Group;
  208.         struct Subject *Sub;
  209.  
  210.         result=0;
  211.  
  212.         while (TRUE)
  213.                 {
  214.                 result=UMSSearchTags(Handle,
  215.                         UMSTAG_SearchLast, result,
  216.                         UMSTAG_SearchDirection, 1,
  217.                         UMSTAG_SearchLocal, TRUE,
  218.                         UMSTAG_SearchMask, (1L<<0),
  219.                         UMSTAG_SearchMatch, (1L<<0),
  220.                         TAG_DONE);
  221.  
  222.                 if (result==0)
  223.                         break;
  224.  
  225.                 Succ=ReadUMSMsgTags(Handle,
  226.                         UMSTAG_RMsgNum, result,
  227.                         UMSTAG_RHeaderLength, &HdrLength,
  228.                         UMSTAG_RTextLength, &TextLength,
  229.                         UMSTAG_RNoUpdate, 1,
  230.                         UMSTAG_RSubject, &Subject,
  231.                         UMSTAG_RGroup, &Group,
  232.                         TAG_DONE);
  233.  
  234.  
  235.                 if (Succ)
  236.                         {
  237.                         sprintf(SubjLine, "%-33s  In: %s", Subject, Group?Group:":::Mail:::");
  238.                         AddSubjectToList(SubjLine);
  239.                         } else
  240.                         printf("UMS-Error: %3d, '%s'\n",UMSErrNum(Handle),UMSErrTxt(Handle));
  241.  
  242.                 if (SetSignal(0,0)&SIGBREAKF_CTRL_C)
  243.                         {
  244.                         cleanup();
  245.                         exit(0);
  246.                         }
  247.                 };
  248.  
  249.         Sub=Subjects;
  250.         while (Sub)
  251.                 {
  252.                 printf("%4d: %s\n", Sub->Count, Sub->Text);
  253.                 Sub=Sub->Next;
  254.                 };
  255.         };
  256.  
  257. cleanup();
  258. return(0);
  259. }
  260.